// pages/userStu/userStu.js
const app = getApp();
const request = require('../../utils/request.js');
const util = require('../../utils/util.js');
const ApiRootUrl = app.globalData.REQUEST_IP_HEAD;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    unit1: [],
    unit2: [],
    user:[],
    collegeTab : 0,
    userName: "",
    display1:"flex",
    display2:"none",
    backImg: ApiRootUrl + "/res/img/miniApp/医疗主页背景.png",
    defaultStuAvator: ApiRootUrl + "/res/img/avatar/学生头像.png"
  },
  /**
   * 获取所有教学单位
   */
  getEnt(){
    var that = this;
    request.get('/ent/getAllStuEnt', {}).then((res) => {
      if (res.code == 200) {
        let colleges = res.data.collegeEnt;
        let majors = res.data.majorEnt;
        for(var i=0;i < colleges.length;i++){
          colleges[i].color = i == 0? "green": "black";
          colleges[i].back = i == 0? "#f1f1f1": "white";
          colleges[i].border = i == 0? "1px solid black": "none";
        }
        for(var i=0;i < majors.length;i++){
          majors[i].display1 = "block";
          majors[i].display2 = "none";
          majors[i].display3 = "none";
        }
        that.setData({
          unit1: colleges,
          unit2: majors,
        })
      } else {
        util.showToast(res.msg, 'none');
      }
    }).catch((errMsg) => {
      util.showToast(errMsg, 'error');
    })
  },
  /**
   * 选择菜单项
   * @param {*} e 
   */
  tabSelect(e){
    var list = this.data.unit1;
    for(var i=0;i<list.length;i++){
      if(i == e.currentTarget.dataset.id){
        list[i].color = "green";
        list[i].back = "#f1f1f1";
        list[i].border = "1px solid black"
      }else{
        list[i].color = "black";
        list[i].back = "white";
        list[i].border = "none"
      }
    }
    this.setData({
      unit1 : list,
      collegeTab : e.currentTarget.dataset.id
    })
  },
  /**
   * 展开
   * @param {*} e 
   */
  open(e){
    var list = this.data.unit2;
    for(var i=0;i<list.length;i++){
      if(i == e.currentTarget.dataset.index){
        list[i].display1 = "none";
        list[i].display2 = "block";
        list[i].display3 = "block";
      }else{
        list[i].display1 = "block";
        list[i].display2 = "none";
        list[i].display3 = "none";
      }
    }
    this.setData({
      unit2 : list,
    })
    this.getUser(this.data.unit2[e.currentTarget.dataset.index].code,"");
  },
  /**
   * 收起
   */
  close(e){
    var list = this.data.unit2;
    list[e.currentTarget.dataset.index].display1 = "block";
    list[e.currentTarget.dataset.index].display2 = "none";
    list[e.currentTarget.dataset.index].display3 = "none";
    this.setData({
      unit2 : list,
      user: []
    })
  },

  /**
   * 获取所有用户
   */
  getUser(entCode,name){
    var that = this;
    request.get('/user/findByEntAndName', {
      entCode: entCode,
      name: name
    }).then((res) => {
      if (res.code == 200) {
        that.setData({
          user: res.data,
        })
      } else {
        util.showToast(res.msg, 'none');
      }
    }).catch((errMsg) => {
      util.showToast(errMsg, 'error');
    })
  },

  /**
   * 搜索用户
   * @param {*} e 
   */
  nameInput(e){
    this.setData({
      userName: e.detail.value.replace(/(^\s*)|(\s*$)/g, "")
    })
  },

  /**
   * 搜索用户
   */
  search(){
    let key = this.data.userName;
    if(key.length == 0){
      wx.showToast({
        title: '请输入用户姓名',
        icon:'error'
      })
      return;
    }
    this.getUser("",key);
    this.setData({
      display1:"none",
      display2:"flex"
    })
  },

  /**
   * 返回
   */
  reBack(){
    this.setData({
      user: [],
      display2:"none",
      display1:"flex"
    })
  },

    /**
   * 跳转到聊天页面
   */
  toChat(e){
    const that = this;
    var you = this.data.user[e.currentTarget.dataset.index];
    // console.log(you);
    wx.navigateTo({
      url: '/pages/chat/chat?youId='+you.id+'&youName='+you.name+'&youUnit='+you.entName+'&youAvatar='+that.data.defaultStuAvator,
    })
  },
  /**
   * 跳转到病人详情页面
   * @param {*} e 
   */
  patientRecord(e){
    // console.log(e);
    var uid = e.currentTarget.dataset.uid;
    // console.log(you);
    wx.navigateTo({
      url: '/pages/ptRecord/ptRecord?id='+uid,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getEnt();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})